//
//  MyRepariImgTableViewCell.m
//  PropertyManager
//
//  Created by TNG on 2018/7/19.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "MyRepariImgTableViewCell.h"

@implementation MyRepariImgTableViewCell

static NSString * MyRepariImgCollectionViewCellID = @"MyRepariImgCollectionViewCell";

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self creatUI];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

-(void)creatUI{
    [self.contentView addSubview:self.collectionView];
}

-(UICollectionView *)collectionView
{
    if (_collectionview == nil)
    {
        UICollectionViewFlowLayout * floweLayout =[[UICollectionViewFlowLayout alloc]init];
        floweLayout.itemSize=CGSizeMake((WIDTH - 70)/4, (WIDTH - 70)/4);
        floweLayout.minimumLineSpacing= 10;
        floweLayout.minimumInteritemSpacing = 0;
        floweLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 14, WIDTH - 40, (WIDTH - 70)/4 + 10 * 1) collectionViewLayout:floweLayout];
        _collectionview.dataSource =self;
        _collectionview.delegate = self;
        _collectionview.bounces = NO;
        _collectionview.backgroundColor=[UIColor whiteColor];
        _collectionview.pagingEnabled = YES;
        _collectionview.showsVerticalScrollIndicator = NO;
        [_collectionview registerClass:[MyRepariImgCollectionViewCell class] forCellWithReuseIdentifier:MyRepariImgCollectionViewCellID];
    }
    return _collectionview;
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imgArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyRepariImgCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MyRepariImgCollectionViewCellID forIndexPath:indexPath];
    if (self.imgArr.count) {
        NSString * str = [self.imgArr objectAtIndex:indexPath.row];
        if ([str containsString:@".mp4"]) {
            cell.img.backgroundColor = SFMRandColor;
        }else if ([str containsString:@".jpg"])
        {
            [cell.img sd_setImageWithURL:[NSURL URLWithString:str] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                cell.img.image = image;
            }];
        }else
        {
            cell.img.backgroundColor = SFMRandColor;
        }
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}

-(void)send
{
    
}

-(void)sendRepariModel:(RepariModel *)model
{
    NSMutableArray * arr = [NSMutableArray array];
    if (model.bxFj1 != nil) {
        [arr addObject:model.bxFj1];
    }
    if (model.bxFj2 != nil) {
        [arr addObject:model.bxFj2];
    }
    
    if (model.bxFj3 != nil) {
        [arr addObject:model.bxFj3];
    }
    self.imgArr = arr;
    [self.collectionview reloadData];
}

-(void)sendImgArr:(NSArray *)arr
{
    self.imgArr = arr;
    [self.collectionview reloadData];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
